home *** CD-ROM | disk | FTP | other *** search
- /*
- * File: MathTool.c
- *
- * Contains: V.U. 2.1 External Tool Interface Apple Event test program.
- * Converted from Chuck Lins pascal version
- *
- * This program receives and responds to Apple Events using the
- * V.U. 2.1 External Tool interface protocol. Additional commentary
- * about handling Apple Events in general can be found in the
- * Inside Macintosh Volume VI.
- *
- * Written by: Automation Systems, STM, MSSW
- *
- * Copyright: © 1992-1994 by Apple Computer, Inc., all rights reserved.
- *
- * Change History (most recent first):
- *
- * <10> 9/14/94 CMW Update for Universal Headers.
- * <9> 1/27/94 BD Relstring moved to TextUtils.h from Packages.h.
- * <8> 1/14/94 CMW Update for Apple Scriptable external tools interface (V.U. 2.1).
- * <?> 10/31/92 REM Added support for new services.
- * 8/6/92 DGG Became MathTool.c from original mathTool.c
- * 8/3/92 DGG Support for Long Integers added
- * 7/30/92 DGG Made to work only with floating point operations.
- * 7/30/92 DGG Times service implemented.
- * <2+> 3/10/92 RV Use relstring for string comparisons
- * <2> 3/6/92 JCC Modifications for new communications methods
- * <8> 2/20/92 JCC Ready for SCM build
- * <7> 2/20/92 JCC Resource changes
- * <6> 2/19/92 JCC Finishing touches
- * <5> 2/18/92 CMW Fix a few more bugs with the command name passing.
- * <4> 2/18/92 JCC I was returning longs to VU. NO NO fluffy!
- * <3> 2/13/92 JCC Mostly debugged. Still has a problem with VUCmdHandler though.
- * <2> 2/12/92 JCC Compiles. Not fully debugged yet.
- *
- * To Do:
- *
- */
-
- // Includes
- #include <Types.h>
- #include <Memory.h>
- #include <TextUtils.h>
- #include <Errors.h>
- #include <QuickDraw.h>
- #include <Fonts.h>
- #include <Dialogs.h>
- #include <Windows.h>
- #include <Menus.h>
- #include <Events.h>
- #include <OSEvents.h>
- #include <Desk.h>
- #include <DiskInit.h>
- #include <Resources.h>
- #include <ToolUtils.h>
- #include <AppleEvents.h>
- #include <EPPC.h>
- #include <GestaltEqu.h>
- #include <PPCToolbox.h>
- #include <Processes.h>
- #include <String.h>
- #include <VUAE.h>
- #include <Math.h>
-
- // Constants
- #define kMBarID 128
- #define kAppleMenu 128
- #define kFileMenu 129
- #define kEditMenu 130
- #define NIL 0L
- #define TRUE 1
- #define FALSE 0
- #define kSuspendResumeMessage 1
- #define kMouseMovedMessage 250
-
- #define kGREATER 1
- #define kLESS -1
- #define kEQUAL 0
- #define kNotANumber -1
-
- // MathTool services
-
- OSErr VUExtendedPlusService( AppleEvent msg, AppleEvent reply, long refCon );
- OSErr VUExtendedMinusService( AppleEvent msg, AppleEvent reply, long refCon );
- OSErr VUExtendedTimesService( AppleEvent msg, AppleEvent reply, long refCon );
- OSErr VUExtendedDivideService( AppleEvent msg, AppleEvent reply, long refCon );
- OSErr VUExtendedCompareService( AppleEvent msg, AppleEvent reply, long refCon );
- //
- OSErr VUSinService( AppleEvent msg, AppleEvent reply, long refCon );
- OSErr VUCosService( AppleEvent msg, AppleEvent reply, long refCon );
- OSErr VUTanService( AppleEvent msg, AppleEvent reply, long refCon );
- OSErr VUASinService( AppleEvent msg, AppleEvent reply, long refCon );
- OSErr VUACosService( AppleEvent msg, AppleEvent reply, long refCon );
- OSErr VUATanService( AppleEvent msg, AppleEvent reply, long refCon );
- //
- OSErr VUSqrtService( AppleEvent msg, AppleEvent reply, long refCon );
- OSErr VUPowerService( AppleEvent msg, AppleEvent reply, long refCon );
- OSErr VULogService( AppleEvent msg, AppleEvent reply, long refCon );
- //
- OSErr VULongPlusService( AppleEvent msg, AppleEvent reply, long refCon );
- OSErr VULongMinusService( AppleEvent msg, AppleEvent reply, long refCon );
- OSErr VULongTimesService( AppleEvent msg, AppleEvent reply, long refCon );
- OSErr VULongDivideService( AppleEvent msg, AppleEvent reply, long refCon );
- OSErr VULongModService( AppleEvent msg, AppleEvent reply, long refCon);
- OSErr VULongCompareService( AppleEvent msg, AppleEvent reply, long refCon);
-
- // VU required services
-
- pascal OSErr VUServiceHandler( AppleEvent msg, AppleEvent reply, long refCon );
-
- OSErr VUInitializeHandler( AppleEvent msg, AppleEvent reply, long refCon );
- OSErr VUCancelHandler( AppleEvent msg, AppleEvent reply, long refCon );
- OSErr VUSrvcListHandler( AppleEvent msg, AppleEvent reply, long refCon );
- OSErr VUVersionHandler( AppleEvent msg, AppleEvent reply, long refCon );
- OSErr VUHasSrvcHandler( AppleEvent msg, AppleEvent reply, long refCon );
- OSErr VUQuitHandler( AppleEvent msg, AppleEvent reply, long refCon );
-
- // AppleEvent required services
-
- pascal OSErr AEOpenHandler( AppleEvent msg, AppleEvent reply, long refCon );
- pascal OSErr AEOpenDocHandler( AppleEvent msg, AppleEvent reply, long refCon );
- pascal OSErr AEQuitHandler( AppleEvent msg, AppleEvent reply, long refCon );
- pascal OSErr AEPrintHandler( AppleEvent msg, AppleEvent reply, long refCon );
-
- // Support
-
- void InitAEStuff(void);
- void InitProgram(void);
- void Idling(void);
- void InvokeDA (MenuHandle theMenu, long theItem);
- void BuildMenuBar(void);
- void MenuCommand (long theCmd);
- void DispatchMouseDown (EventRecord theEvent);
- void DispatchKeyDown (EventRecord theEvent);
- void ActivateWindow (WindowPtr theWindow, Boolean activating);
- void UpdateWindow (WindowPtr theWindow);
- void DiskInserted (long diskInfo);
- void DispatchOSEvent (EventRecord theEvent);
- void DispatchHighEvent (EventRecord theEvent);
- pascal OSErr VUErrorReply (AppleEvent *reply, Str255 errorText, OSErr errorNo);
- void ReportError (OSErr err, long where);
- void SrvcError (Str255 msg);
-
- // Globals
-
- Boolean hasAE, quitting, background;
- Handle theMenuBar;
- MenuHandle appleMenu, fileMenu, editMenu;
- EventRecord theEvent;
- OSErr aeError;
- Str255 strTemp1, strTemp2;
-
-
- // Service Table definition
-
- typedef struct {
- char* serviceName;
- OSErr (*serviceFunction)( AppleEvent, AppleEvent, long );
- } ServiceInfo;
-
- ServiceInfo VU_Tool_Services[] = {
- { "Initialize", VUInitializeHandler },
- { "Cancel", VUCancelHandler },
- { "GetToolServices", VUSrvcListHandler },
- { "GetToolVersion", VUVersionHandler },
- { "ServiceSupported", VUHasSrvcHandler },
- { "Quit", VUQuitHandler },
- //
- { "fplus", VUExtendedPlusService },
- { "fminus", VUExtendedMinusService },
- { "ftimes", VUExtendedTimesService },
- { "fdivide", VUExtendedDivideService },
- { "fcompare", VUExtendedCompareService },
- //
- { "sin", VUSinService },
- { "cos", VUCosService },
- { "tan", VUTanService },
- { "asin", VUASinService },
- { "acos", VUACosService },
- { "atan", VUATanService },
- //
- { "sqrt", VUSqrtService },
- { "power", VUPowerService },
- { "ln", VULogService },
- //
- { "lplus", VULongPlusService },
- { "lminus", VULongMinusService },
- { "ltimes", VULongTimesService },
- { "ldivide", VULongDivideService},
- { "lcompare", VULongCompareService },
- { "lmod", VULongModService }
- };
-
- const long NumberOfServices = (sizeof( VU_Tool_Services ) / sizeof( ServiceInfo ));
-
-
- // ** Functions **
- main()
- {
- // Main event loop. Just get events, and dispatch them to an
- // appropriate handler until we get the 'quit' command.
-
- InitProgram();
-
- while (!quitting) {
- WaitNextEvent (everyEvent, &theEvent, 30, NIL);
- switch (theEvent.what) {
- case nullEvent:
- Idling;
- break;
- case mouseDown:
- DispatchMouseDown(theEvent);
- break;
- case keyDown:
- case autoKey:
- DispatchKeyDown(theEvent);
- break;
- case activateEvt:
- ActivateWindow((WindowPtr)theEvent.message,
- (theEvent.modifiers & activeFlag) != 0);
- break;
- case updateEvt:
- UpdateWindow((WindowPtr)theEvent.message);
- break;
- case diskEvt:
- DiskInserted(theEvent.message);
- break;
- case osEvt:
- DispatchOSEvent(theEvent);
- break;
- case kHighLevelEvent:
- DispatchHighEvent(theEvent);
- break;
- }
- }
- }
-
-
- OSErr ExtractStringFromAEList( AEDescList paramList, long index, char* string)
- {
- OSErr aeErr;
- AEKeyword paramKeyword;
- DescType actualType;
- Size actualSize;
-
- aeErr = AEGetNthPtr( ¶mList,
- index,
- typeChar,
- ¶mKeyword,
- &actualType,
- (Ptr) string,
- 255,
- &actualSize );
- if( aeErr )
- {
- ReportError( aeErr, 13 );
- }
- else
- {
- string[actualSize] = '\0';
- }
-
- return( aeErr );
- }
-
-
- pascal OSErr VUServiceHandler(AppleEvent msg, AppleEvent reply, long refcon)
- {
- OSErr aeErr;
- Str255 serviceName = "";
- DescType actualType;
- Size actualSize;
- long i;
-
- // Get the AE Service Name
- aeErr = AEGetParamPtr(&msg,
- kVUAESrvcName,
- typeChar,
- &actualType,
- serviceName,
- 255,
- &actualSize);
- ReportError(aeErr, 1);
- if( aeErr == noErr )
- {
- serviceName[ actualSize ] = '\0';
- }
-
- for( i = 0; i < NumberOfServices; i++ )
- {
- if( !relstring( serviceName, VU_Tool_Services[ i ].serviceName, false, true ) )
- {
- aeErr = (*(VU_Tool_Services[ i ].serviceFunction))( msg, reply, refcon );
- return( aeErr );
- }
- }
-
- return( errAEUnknownService);
- }
-
- OSErr VUInitializeHandler( AppleEvent /*msg*/, AppleEvent /*reply*/, long /*refCon*/ )
- {
- return noErr;
- }
-
- OSErr VUCancelHandler( AppleEvent /*msg*/, AppleEvent /*reply*/, long /*refCon*/ )
- {
- return noErr;
- }
-
- OSErr VUQuitHandler( AppleEvent /*msg*/, AppleEvent /*reply*/, long /*refCon*/ )
- {
- quitting = TRUE;
- return noErr;
- }
-
- OSErr VUVersionHandler( AppleEvent, AppleEvent reply, long )
- {
- // This Apple Event handler responds to the messages with event class 'v.u.'
- // and event id 'vers'. This AppleEvent asks an external tool to respond
- // with version information about itself.
-
- // The tool name is used by V.U. 2.0 as the name of a tool as shown to a user.
- // Thus, this should be something descriptive; e.g., 'Screen Capture Tool'.
-
- // The version number is simply a number returned to the caller indicating
- // the version of the tool. A V.U. 2.0 script writer could perhaps used this
- // information in verifying that the current release of an external tool was
- // present.
-
- // The version string simply allows more information to be sent by the external
- // tool. V.U. 2.0 does not use the version string.
-
- // This routine simply stuffs values into
- // the Apple Event reply using Apple Event Manager routines.
-
- OSErr aeErr;
- AEDescList srvcList;
- char* strPtr;
-
- aeErr = AECreateList( NIL, 0, FALSE, &srvcList );
- if( aeErr )
- {
- ReportError( aeErr, 3 );
- return( aeErr );
- }
-
-
- strPtr = "MathTool";
- aeErr = AEPutPtr( &srvcList,
- 1,
- typeChar,
- strPtr,
- strlen( strPtr ) );
- if( aeErr )
- {
- ReportError( aeErr, 4 );
- }
- else
- {
- strPtr = "2.0";
- aeErr = AEPutPtr( &srvcList,
- 2,
- typeChar,
- strPtr,
- strlen( strPtr ) );
- if( aeErr )
- {
- ReportError( aeErr, 5 );
- }
- else
- {
- strPtr = "MathTool 2.0 by Automation Systems, Apple Computer, Inc.";
- aeErr = AEPutPtr( &srvcList,
- 3,
- typeChar,
- strPtr,
- strlen( strPtr ) );
- if( aeErr )
- {
- ReportError( aeErr, 6 );
- }
- else
- {
- aeErr = AEPutKeyDesc( &reply,
- kVUAESrvcResults,
- &srvcList );
- if( aeErr )
- {
- ReportError( aeErr, 7 );
- }
- }
- }
- }
-
- AEDisposeDesc( &srvcList );
- return( aeErr );
- }
-
-
- OSErr VUHasSrvcHandler( AppleEvent msg, AppleEvent reply, long )
- {
- // This Service asks an external tool whether a
- // service with a specific name is supported by the tool.
-
- // The routine first extracts the service name from the Apple Event. If the
- // service name cannot be extracted for some reason from the Apple Event,
- // a standard error reply message is sent back to V.U. 2.0.
-
- // The routine simply returns the AppleEvent standard types 'typeTrue' or
- // 'typeFalse' depending on whether the service name is supported by the
- // tool or not. The Apple Event Manager will coerce these standard types
- // into an Apple Event Boolean value if necessary (and requested by V.U. 2.0).
-
- OSErr aeErr;
- Str255 serviceName;
- long i;
-
- AEDescList paramList;
- long numParams;
-
-
- aeErr = AEGetParamDesc( &msg,
- kVUAESrvcParameters,
- typeAEList,
- ¶mList );
- if( aeErr )
- {
- ReportError( aeErr, 14 );
- return( aeErr );
- }
-
- if( aeErr = AECountItems( ¶mList, &numParams ) )
- {
- ReportError( aeErr, 15 );
- }
- else if( numParams != 1 )
- {
- if( aeErr = VUErrorReply( &reply,
- "Incorrect number of parameters supplied!",
- errAEWrongParameters ) )
- {
- ReportError( aeErr, 16 );
- }
- aeErr = errAEWrongParameters;
- }
- else if( aeErr = ExtractStringFromAEList( paramList, 1, serviceName ) )
- {
- ReportError( aeErr, 17 );
- }
-
- AEDisposeDesc( ¶mList );
-
- if( aeErr )
- {
- return( aeErr );
- }
-
- for( i = 0; i < NumberOfServices; i++ )
- {
- if( !relstring( serviceName, VU_Tool_Services[ i ].serviceName, false, true ) )
- {
- aeErr = AEPutParamPtr( &reply,
- kVUAESrvcResults,
- typeTrue,
- NIL,
- 0);
- return( aeErr );
- }
- }
-
- aeErr = AEPutParamPtr( &reply,
- kVUAESrvcResults,
- typeFalse,
- NIL,
- 0);
- if( aeErr )
- {
- ReportError(aeErr, 9);
- return( aeErr );
- }
-
- return(aeErr);
- }
-
-
- OSErr VUSrvcListHandler( AppleEvent, AppleEvent reply, long )
- {
- // This Apple Event handler responds to the messages with event class 'v.u.'
- // and event id 'cmdl'. This AppleEvent asks an external tool for a list of
- // all service names supported by the tool.
-
- // Remember that Apple Event Lists are all one (1) based; i.e., indexes
- // start from one (not zero).
-
- // The first step creates an Apple Event List descriptor. This is followed by
- // stuffing each service name into the list, starting at index number one.
- // Once the list if filled with the service names, the list itself is inserted
- // into the Apple Event reply.
-
- OSErr aeErr;
- AEDescList srvcList;
- char* strPtr;
- long i;
-
- aeErr = AECreateList( NIL, 0, FALSE, &srvcList );
- if( aeErr )
- {
- ReportError( aeErr, 10 );
- return( aeErr );
- }
-
- for( i = 0; i < NumberOfServices; i++ )
- {
- strPtr = VU_Tool_Services[ i ].serviceName;
- aeErr = AEPutPtr( &srvcList,
- i + 1,
- typeChar,
- strPtr,
- strlen( strPtr ) );
- if( aeErr )
- {
- ReportError( aeErr, 11 );
- AEDisposeDesc( &srvcList );
- return( aeErr );
- }
- }
-
-
- aeErr = AEPutKeyDesc( &reply,
- kVUAESrvcResults,
- &srvcList );
- if( aeErr )
- {
- ReportError( aeErr, 12 );
- }
-
- AEDisposeDesc( &srvcList );
-
- return( aeErr );
- }
-
-
- OSErr ExtractExtendedFromAEList( AEDescList paramList, long index, extended* result )
- {
- OSErr aeErr;
- AEKeyword paramKeyword;
- DescType actualType;
- Size actualSize;
-
- aeErr = AEGetNthPtr( ¶mList,
- index,
- typeExtended,
- ¶mKeyword,
- &actualType,
- (Ptr) result,
- sizeof( *result ),
- &actualSize );
- if( aeErr )
- {
- ReportError( aeErr, 13 );
- }
-
- return( aeErr );
- }
-
- OSErr ExtractLongIntegerFromAEList( AEDescList paramList, long index, long* result )
- {
- OSErr aeErr;
- AEKeyword paramKeyword;
- DescType actualType;
- Size actualSize;
-
- aeErr = AEGetNthPtr( ¶mList,
- index,
- typeLongInteger,
- ¶mKeyword,
- &actualType,
- (Ptr) result,
- sizeof( *result ),
- &actualSize );
- if( aeErr )
- {
- ReportError( aeErr, 13 );
- }
-
- return( aeErr );
- }
-
- // ExtractExtendedPairFromAEList will get the firstNumber and secondNumber with
- // error checking from a given AppleEvent list.
- OSErr ExtractExtendedPairFromAEList( extended* firstNumber, extended* secondNumber,
- AppleEvent msg, AppleEvent reply)
- {
- OSErr aeErr;
- AEDescList paramList;
- long numParams;
-
- aeErr = AEGetParamDesc( &msg,
- kVUAESrvcParameters,
- typeAEList,
- ¶mList );
- if( aeErr )
- {
- ReportError( aeErr, 14 );
- return( aeErr );
- }
-
- if( aeErr = AECountItems( ¶mList, &numParams ) )
- {
- ReportError( aeErr, 15 );
- }
- else if( numParams != 2 )
- {
- if( aeErr = VUErrorReply( &reply,
- "wrong number of parameters",
- errAEWrongParameters ) )
- {
- ReportError( aeErr, 16 );
- }
- aeErr = errAEWrongParameters;
- }
- else if( aeErr = ExtractExtendedFromAEList( paramList, 1, firstNumber ) )
- {
- ReportError( aeErr, 17 );
- }
- else if( aeErr = ExtractExtendedFromAEList( paramList, 2, secondNumber ) )
- {
- ReportError( aeErr, 18 );
- }
-
- AEDisposeDesc( ¶mList );
-
- if( aeErr )
- {
- return( aeErr );
- }
- }
-
- // ExtractExtendedSingleFromAEList will get a single number with
- // error checking from a given AppleEvent list.
- OSErr ExtractExtendedSingleFromAEList( extended* theNumber, AppleEvent msg, AppleEvent reply)
- {
- OSErr aeErr;
- AEDescList paramList;
- long numParams;
-
- aeErr = AEGetParamDesc( &msg,
- kVUAESrvcParameters,
- typeAEList,
- ¶mList );
- if( aeErr )
- {
- ReportError( aeErr, 14 );
- return( aeErr );
- }
-
- if( aeErr = AECountItems( ¶mList, &numParams ) )
- {
- ReportError( aeErr, 15 );
- }
- else if( numParams != 1 )
- {
- if( aeErr = VUErrorReply( &reply,
- "wrong number of parameters",
- errAEWrongParameters ) )
- {
- ReportError( aeErr, 16 );
- }
- aeErr = errAEWrongParameters;
- }
- else if( aeErr = ExtractExtendedFromAEList( paramList, 1, theNumber ) )
- {
- ReportError( aeErr, 17 );
- }
-
- AEDisposeDesc( ¶mList );
-
- if( aeErr )
- {
- return( aeErr );
- }
- }
-
- // ExtractLongIntegerFromAEList will get the firstNumber and secondNumber with
- // error checking from a given AppleEvent list.
- OSErr ExtractLongIntegerPairFromAEList( long* firstNumber, long* secondNumber,
- AppleEvent msg, AppleEvent reply)
- {
- OSErr aeErr;
- AEDescList paramList;
- long numParams;
-
- aeErr = AEGetParamDesc( &msg,
- kVUAESrvcParameters,
- typeAEList,
- ¶mList );
- if( aeErr )
- {
- ReportError( aeErr, 14 );
- return( aeErr );
- }
-
- if( aeErr = AECountItems( ¶mList, &numParams ) )
- {
- ReportError( aeErr, 15 );
- }
- else if( numParams != 2 )
- {
- if( aeErr = VUErrorReply( &reply,
- "wrong number of parameters",
- errAEWrongParameters ) )
- {
- ReportError( aeErr, 16 );
- }
- aeErr = errAEWrongParameters;
- }
- else if( aeErr = ExtractLongIntegerFromAEList( paramList, 1, firstNumber ) )
- {
- ReportError( aeErr, 17 );
- }
- else if( aeErr = ExtractLongIntegerFromAEList( paramList, 2, secondNumber ) )
- {
- ReportError( aeErr, 18 );
- }
-
- AEDisposeDesc( ¶mList );
-
- if( aeErr )
- {
- return( aeErr );
- }
- }
-
- // VUExtendedPlusService will carry out the request to do
- // addition with floating point numbers.
- OSErr VUExtendedPlusService( AppleEvent msg, AppleEvent reply, long )
- {
- OSErr aeErr;
- extended leftNumber, rightNumber, result;
-
-
- aeErr = ExtractExtendedPairFromAEList(&leftNumber, &rightNumber, msg, reply);
- if( aeErr )
- {
- return( aeErr );
- }
-
- result = leftNumber + rightNumber;
-
- aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
- (Ptr) &result, sizeof( result ) );
- if( aeErr )
- {
- ReportError( aeErr, 19 );
- return( aeErr );
- }
-
- return( aeErr );
- }
-
-
- // VUExtendedMinusService will carry out the request to do
- // subtraction with floating point numbers.
- OSErr VUExtendedMinusService( AppleEvent msg, AppleEvent reply, long )
- {
- OSErr aeErr;
- extended leftNumber, rightNumber, result;
-
-
- aeErr = ExtractExtendedPairFromAEList(&leftNumber, &rightNumber, msg, reply);
- if( aeErr )
- {
- return( aeErr );
- }
-
- result = leftNumber - rightNumber;
-
- aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
- (Ptr) &result, sizeof( result ) );
- if( aeErr )
- {
- ReportError( aeErr, 19 );
- return( aeErr );
- }
-
- return( aeErr );
- }
-
- // The VUExtendedTimesService will carry out the request to do
- // multiplication on floating point numbers.
- OSErr VUExtendedTimesService( AppleEvent msg, AppleEvent reply, long )
- {
- OSErr aeErr;
- extended leftNumber, rightNumber, result;
-
-
- aeErr = ExtractExtendedPairFromAEList(&leftNumber, &rightNumber, msg, reply);
- if( aeErr )
- {
- return( aeErr );
- }
-
- result = leftNumber * rightNumber;
-
- aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
- (Ptr) &result, sizeof( result ) );
- if( aeErr )
- {
- ReportError( aeErr, 19 );
- return( aeErr );
- }
-
- return( aeErr );
- }
-
- // The VUExtendedDivideService will carry out the request to do
- // division on floating point numbers.
- OSErr VUExtendedDivideService( AppleEvent msg, AppleEvent reply, long )
- {
- OSErr aeErr;
- extended leftNumber, rightNumber, result;
-
-
- aeErr = ExtractExtendedPairFromAEList(&leftNumber, &rightNumber, msg, reply);
- if( aeErr )
- {
- return( aeErr );
- }
-
- result = leftNumber / rightNumber;
-
- aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
- (Ptr) &result, sizeof( result ) );
- if( aeErr )
- {
- ReportError( aeErr, 19 );
- return( aeErr );
- }
-
- return( aeErr );
- }
-
- // The VUExtendedCompareService will carry out the request to do
- // comparison on floating point numbers.
- OSErr VUExtendedCompareService( AppleEvent msg, AppleEvent reply, long )
- {
- OSErr aeErr;
- extended leftNumber, rightNumber;
- short result;
-
-
- aeErr = ExtractExtendedPairFromAEList(&leftNumber, &rightNumber, msg, reply);
- if( aeErr )
- {
- return( aeErr );
- }
-
- if (leftNumber > rightNumber)
- result = kGREATER;
- else if (leftNumber < rightNumber)
- result = kLESS;
- else
- result = kEQUAL;
-
- aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeShortInteger,
- (Ptr) &result, sizeof( result ) );
- if( aeErr )
- {
- ReportError( aeErr, 19 );
- return( aeErr );
- }
-
- return( aeErr );
- }
-
- // The VUSinService will carry out the request to do
- // sine operation on a given number.
- OSErr VUSinService( AppleEvent msg, AppleEvent reply, long )
- {
- OSErr aeErr;
- extended theNumber, result;
-
-
- aeErr = ExtractExtendedSingleFromAEList(&theNumber, msg, reply);
- if( aeErr )
- {
- return( aeErr );
- }
-
- result = sin(theNumber);
-
- aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
- (Ptr) &result, sizeof( result ) );
- if( aeErr )
- {
- ReportError( aeErr, 19 );
- return( aeErr );
- }
-
- return( aeErr );
- }
-
- // The VUCosService will carry out the request to do
- // cosine operation on a given number.
- OSErr VUCosService( AppleEvent msg, AppleEvent reply, long )
- {
- OSErr aeErr;
- extended theNumber, result;
-
-
- aeErr = ExtractExtendedSingleFromAEList(&theNumber, msg, reply);
- if( aeErr )
- {
- return( aeErr );
- }
-
- result = cos(theNumber);
-
- aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
- (Ptr) &result, sizeof( result ) );
- if( aeErr )
- {
- ReportError( aeErr, 19 );
- return( aeErr );
- }
-
- return( aeErr );
- }
-
- // The VUTanService will carry out the request to do
- // tangent operation on a given number.
- OSErr VUTanService( AppleEvent msg, AppleEvent reply, long )
- {
- OSErr aeErr;
- extended theNumber, result;
-
-
- aeErr = ExtractExtendedSingleFromAEList(&theNumber, msg, reply);
- if( aeErr )
- {
- return( aeErr );
- }
-
- result = tan(theNumber);
-
- aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
- (Ptr) &result, sizeof( result ) );
- if( aeErr )
- {
- ReportError( aeErr, 19 );
- return( aeErr );
- }
-
- return( aeErr );
- }
-
- // The VUASinService will carry out the request to do
- // arcsine operation on a given number.
- OSErr VUASinService( AppleEvent msg, AppleEvent reply, long )
- {
- OSErr aeErr;
- extended theNumber, result;
-
-
- aeErr = ExtractExtendedSingleFromAEList(&theNumber, msg, reply);
- if( aeErr )
- {
- return( aeErr );
- }
-
- result = asin(theNumber);
-
- aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
- (Ptr) &result, sizeof( result ) );
- if( aeErr )
- {
- ReportError( aeErr, 19 );
- return( aeErr );
- }
-
- return( aeErr );
- }
-
- // The VUACosService will carry out the request to do
- // arccosine operation on a given number.
- OSErr VUACosService( AppleEvent msg, AppleEvent reply, long )
- {
- OSErr aeErr;
- extended theNumber, result;
-
-
- aeErr = ExtractExtendedSingleFromAEList(&theNumber, msg, reply);
- if( aeErr )
- {
- return( aeErr );
- }
-
- result = acos(theNumber);
-
- aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
- (Ptr) &result, sizeof( result ) );
- if( aeErr )
- {
- ReportError( aeErr, 19 );
- return( aeErr );
- }
-
- return( aeErr );
- }
-
- // The VUATanService will carry out the request to do
- // arctangent operation on a given number.
- OSErr VUATanService( AppleEvent msg, AppleEvent reply, long )
- {
- OSErr aeErr;
- extended theNumber, result;
-
-
- aeErr = ExtractExtendedSingleFromAEList(&theNumber, msg, reply);
- if( aeErr )
- {
- return( aeErr );
- }
-
- result = atan(theNumber);
-
- aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
- (Ptr) &result, sizeof( result ) );
- if( aeErr )
- {
- ReportError( aeErr, 19 );
- return( aeErr );
- }
-
- return( aeErr );
- }
-
- // The VUSqrtService will carry out the request to do
- // a square root on a given number.
- OSErr VUSqrtService( AppleEvent msg, AppleEvent reply, long )
- {
- OSErr aeErr;
- extended theNumber, result;
-
-
- aeErr = ExtractExtendedSingleFromAEList(&theNumber, msg, reply);
- if( aeErr )
- {
- return( aeErr );
- }
-
- result = sqrt(theNumber);
-
- aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
- (Ptr) &result, sizeof( result ) );
- if( aeErr )
- {
- ReportError( aeErr, 19 );
- return( aeErr );
- }
-
- return( aeErr );
- }
-
- // VUPowerService will carry out the request to do
- // a power operation with two numbers.
- OSErr VUPowerService( AppleEvent msg, AppleEvent reply, long )
- {
- OSErr aeErr;
- extended theNumber, thePower, result;
-
-
- aeErr = ExtractExtendedPairFromAEList(&theNumber, &thePower, msg, reply);
- if( aeErr )
- {
- return( aeErr );
- }
-
- result = pow(theNumber, thePower);
-
- aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
- (Ptr) &result, sizeof( result ) );
- if( aeErr )
- {
- ReportError( aeErr, 19 );
- return( aeErr );
- }
-
- return( aeErr );
- }
-
- // The VULogService will carry out the request to do
- // natural logarithm operation on a given number.
- OSErr VULogService( AppleEvent msg, AppleEvent reply, long )
- {
- OSErr aeErr;
- extended theNumber, result;
-
-
- aeErr = ExtractExtendedSingleFromAEList(&theNumber, msg, reply);
- if( aeErr )
- {
- return( aeErr );
- }
-
- result = log(theNumber);
-
- aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeExtended,
- (Ptr) &result, sizeof( result ) );
- if( aeErr )
- {
- ReportError( aeErr, 19 );
- return( aeErr );
- }
-
- return( aeErr );
- }
-
- // VULongPlusService will carry out the request to do
- // addition with long integers.
- OSErr VULongPlusService( AppleEvent msg, AppleEvent reply, long )
- {
- OSErr aeErr;
- long leftNumber, rightNumber, result;
-
-
- aeErr = ExtractLongIntegerPairFromAEList(&leftNumber, &rightNumber, msg, reply);
- if( aeErr )
- {
- if( aeErr = VUErrorReply( &reply,
- "One of the parameters is not a number!",
- kNotANumber ) );
- return( aeErr );
- }
-
- result = leftNumber + rightNumber;
-
- aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeLongInteger,
- (Ptr) &result, sizeof( result ) );
- if( aeErr )
- {
- ReportError( aeErr, 19 );
- return( aeErr );
- }
-
- return( aeErr );
- }
-
- // VULongMinusService will carry out the request to do
- // subtraction with long integers.
- OSErr VULongMinusService( AppleEvent msg, AppleEvent reply, long )
- {
- OSErr aeErr;
- long leftNumber, rightNumber, result;
-
-
- aeErr = ExtractLongIntegerPairFromAEList(&leftNumber, &rightNumber, msg, reply);
- if( aeErr )
- {
- return( aeErr );
- }
-
- result = leftNumber - rightNumber;
-
- aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeLongInteger,
- (Ptr) &result, sizeof( result ) );
- if( aeErr )
- {
- ReportError( aeErr, 19 );
- return( aeErr );
- }
-
- return( aeErr );
- }
-
- // The VULongTimesService will carry out the request to do
- // multiplication on long integers.
- OSErr VULongTimesService( AppleEvent msg, AppleEvent reply, long )
- {
- OSErr aeErr;
- long leftNumber, rightNumber, result;
-
-
- aeErr = ExtractLongIntegerPairFromAEList(&leftNumber, &rightNumber, msg, reply);
- if( aeErr )
- {
- return( aeErr );
- }
-
- result = leftNumber * rightNumber;
-
- aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeLongInteger,
- (Ptr) &result, sizeof( result ) );
- if( aeErr )
- {
- ReportError( aeErr, 19 );
- return( aeErr );
- }
-
- return( aeErr );
- }
-
- // The VULongDivideService will carry out the request to do
- // division on long integers.
- OSErr VULongDivideService( AppleEvent msg, AppleEvent reply, long )
- {
- OSErr aeErr;
- long leftNumber, rightNumber, result;
-
-
- aeErr = ExtractLongIntegerPairFromAEList(&leftNumber, &rightNumber, msg, reply);
- if( aeErr )
- {
- return( aeErr );
- }
-
- result = leftNumber / rightNumber;
-
- aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeLongInteger,
- (Ptr) &result, sizeof( result ) );
- if( aeErr )
- {
- ReportError( aeErr, 19 );
- return( aeErr );
- }
-
- return( aeErr );
- }
-
- // The VULongModService will carry out the request to do
- // mod on long integers.
- OSErr VULongModService( AppleEvent msg, AppleEvent reply, long )
- {
- OSErr aeErr;
- long leftNumber, rightNumber, result;
-
-
- aeErr = ExtractLongIntegerPairFromAEList(&leftNumber, &rightNumber, msg, reply);
- if( aeErr )
- {
- return( aeErr );
- }
-
- result = leftNumber % rightNumber;
-
- aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeLongInteger,
- (Ptr) &result, sizeof( result ) );
- if( aeErr )
- {
- ReportError( aeErr, 19 );
- return( aeErr );
- }
-
- return( aeErr );
- }
-
- // The VULongCompareService will carry out the request to do
- // comparison on long integers.
- OSErr VULongCompareService( AppleEvent msg, AppleEvent reply, long )
- {
- OSErr aeErr;
- long leftNumber, rightNumber;
- short result;
-
-
- aeErr = ExtractLongIntegerPairFromAEList(&leftNumber, &rightNumber, msg, reply);
- if( aeErr )
- {
- return( aeErr );
- }
-
- if (leftNumber > rightNumber)
- result = kGREATER;
- else if (leftNumber < rightNumber)
- result = kLESS;
- else
- result = kEQUAL;
-
- aeErr = AEPutParamPtr( &reply, kVUAESrvcResults, typeShortInteger,
- (Ptr) &result, sizeof( result ) );
- if( aeErr )
- {
- ReportError( aeErr, 19 );
- return( aeErr );
- }
-
- return( aeErr );
- }
-
-
- pascal OSErr AEOpenHandler(AppleEvent, AppleEvent, long)
- {
- // Standard (empty) handler for the 'oapp' Apple Event. Our program does
- // not need anything special here, so "noErr" can simply be returned.
-
- return(noErr);
- }
-
-
- pascal OSErr AEOpenDocHandler(AppleEvent, AppleEvent, long)
- {
- // Standard (empty) handler for the 'odoc' Apple Event. Our program does
- // not have documents, so we ignore this Apple Event.
-
- return(noErr);
- }
-
-
- pascal OSErr AEQuitHandler(AppleEvent, AppleEvent, long)
- {
- // Standard handler for the 'quit' Apple Event. You must never, ever call
- // ExitToShell from within an Apple Event handler. It is certain death
- // for your application. Thus, we just set a flag which is examined later
- // in the main event loop.
-
- quitting = TRUE;
- return(noErr);
- }
-
-
- pascal OSErr AEPrintHandler(AppleEvent, AppleEvent, long)
- {
- // Standard (empty) handler for the 'pdoc' Apple Event. Our program does
- // not have documents, so we ignore this Apple Event.
-
- return(noErr);
- }
-
-
- void ReportError(OSErr err, long where)
- {
- // Reports an error by way of an Alert dialog. "err" is the error
- // code. "where" is an arbitrary (but unique) number indicating
- // where in the program the error occurred. Of course, if there
- // is no error, this routine does nothing.
-
- int dontCare;
- Str255 errStr, whereStr;
-
- if (err != 0) {
- numtostring(err, errStr);
- numtostring(where, whereStr);
- paramtext(errStr, whereStr, '', '');
- dontCare = Alert(128, NIL);
- }
- }
-
-
- void SrvcError(Str255 msg)
- {
- // Displays an error dialog when the service name sent by V.U. 2.0
- // does not conform to a legal service understood by the external
- // tool. In reality, this program should check the Apple Event
- // user interaction level to determine how (and whether) to interact
- // with the user.
-
- int dontCare;
-
- paramtext(msg, '', '', '');
- dontCare = Alert(128, NIL);
- }
-
- pascal OSErr VUErrorReply(AppleEvent *reply, Str255 errorText, OSErr errorNo)
- {
- OSErr aeErr;
-
- aeErr = AEPutParamPtr(reply,
- keyErrorString,
- typeChar,
- errorText,
- strlen(errorText));
- if (aeErr == noErr) {
- aeErr = AEPutParamPtr(reply,
- keyErrorNumber,
- typeShortInteger,
- (Ptr)&errorNo,
- sizeof(errorNo));
- }
- return(aeErr);
- }
-
-
- void InitAEStuff(void)
- {
- // Initialization of the Apple Event Manager and dispatch table.
- // If we don't have Apple Events or installation of any of the
- // Apple Event handlers fails, the program is simply terminated.
- // The routine starts by installing standard handlers for the
- // core Apple Events. This is followed by installation of the
- // event handlers for the V.U. 2.0 external tool interface.
-
- long response;
- OSErr aeErr;
-
- hasAE = (Gestalt(gestaltAppleEventsAttr, &response) == noErr);
- if (hasAE) { // Has AE
- aeErr = AEInstallEventHandler(kVUAETool,
- kVUAESendService,
- (AEEventHandlerProcPtr) VUServiceHandler,
- 0, FALSE);
- if (aeErr != noErr) ExitToShell();
- aeErr = AEInstallEventHandler(kCoreEventClass,
- kAEOpenApplication,
- (AEEventHandlerProcPtr) AEOpenHandler,
- 0, FALSE);
- if (aeErr != noErr) ExitToShell();
- aeErr = AEInstallEventHandler(kCoreEventClass,
- kAEOpenDocuments,
- (AEEventHandlerProcPtr) AEOpenDocHandler,
- 0, FALSE);
- if (aeErr != noErr) ExitToShell();
- aeErr = AEInstallEventHandler(kCoreEventClass,
- kAEQuitApplication,
- (AEEventHandlerProcPtr) AEQuitHandler,
- 0, FALSE);
- if (aeErr != noErr) ExitToShell();
- aeErr = AEInstallEventHandler(kCoreEventClass,
- kAEPrintDocuments,
- (AEEventHandlerProcPtr) AEPrintHandler,
- 0, FALSE);
- if (aeErr != noErr) ExitToShell();
- aeErr = AESetInteractionAllowed(kAEInteractWithAll);
- if (aeErr != noErr) ExitToShell();
- }
- else
- ExitToShell();
- }
-
-
- void BuildMenuBar(void)
- {
- // Construct a menu bar for our application. (Yawn)
-
- theMenuBar = GetNewMBar(kMBarID);
- SetMenuBar(theMenuBar);
- appleMenu = GetMHandle(kAppleMenu);
- fileMenu = GetMHandle(kFileMenu);
- editMenu = GetMHandle(kEditMenu);
- AddResMenu(appleMenu, 'DRVR');
- DrawMenuBar();
- }
-
-
- void InitProgram(void)
- {
- // Initialize the Macintoshâ„¢ Toolbox and application
- // environment.
-
- quitting = FALSE;
- background = FALSE;
- MaxApplZone();
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NIL);
- InitCursor();
-
- InitAEStuff();
- BuildMenuBar();
- }
-
-
- void Idling(void)
- {
- // We don't do anything while idling. Perhaps we should sing a song?
- }
-
-
- void InvokeDA(MenuHandle theMenu, long theItem)
- {
- // Bring up a desk accessory.
-
- OSErr err;
- Str255 da;
-
- GetItem(theMenu, theItem, da);
- err = OpenDeskAcc(da);
- }
-
-
- void MenuCommand(long theCmd)
- {
- // Activate the appropriate menu item.
-
- long menuID, menuItem;
- int dontCare;
-
- menuID = HiWord(theCmd);
- menuItem = LoWord(theCmd);
- switch (menuID ) {
- case kAppleMenu:
- if (menuItem != 1) InvokeDA(appleMenu, menuItem);
- else dontCare = Alert(666, NIL);
- break;
- case kFileMenu:
- quitting = TRUE;
- break;
- case kEditMenu: break;
- }
- HiliteMenu(0);
- }
-
-
- void DispatchMouseDown(EventRecord theEvent)
- {
- // Dispatch a mouse down event to the proper handler.
-
- WindowPtr theWindow;
-
- switch (FindWindow(theEvent.where, &theWindow)) {
- case inMenuBar:
- MenuCommand(MenuSelect(theEvent.where));
- break;
- case inSysWindow:
- SystemClick(&theEvent, theWindow);
- break;
- case inDrag:
- if (theWindow == FrontWindow())
- DragWindow(theWindow, theEvent.where, &qd.screenBits.bounds);
- break;
- case inContent: case inDesk: case inGrow: case inGoAway:
- break;
- }
- }
-
-
- void DispatchKeyDown(EventRecord theEvent)
- {
- // Dispatch a key down event to the proper handler. Since typing
- // isn't supported, the routine only checks for service key equivalents.
-
- char key;
-
- key = theEvent.message & charCodeMask;
- if ((theEvent.modifiers & cmdKey) && (theEvent.what == keyDown))
- MenuCommand(MenuKey(key));
- }
-
-
- void ActivateWindow(WindowPtr, Boolean)
- {
- // Gee, we don't have any windows worth activating.
- }
-
-
- void UpdateWindow(WindowPtr)
- {
- // Gee, we don't have any windows worth updating.
- }
-
- void DiskInserted(long diskInfo)
- {
- // Handle disk insert errors.
-
- Point where;
- OSErr dontCare;
-
- if (HiWord(diskInfo) != noErr) {
- SetPt(&where, 40, 40);
- dontCare = DIBadMount(where, diskInfo);
- }
- }
-
-
- void DispatchOSEvent(EventRecord theEvent)
- {
- // Dispatch OS events.
-
- switch ((theEvent.message >> 24) & 0x0FF) {
- case kMouseMovedMessage:
- Idling();
- break;
-
- case kSuspendResumeMessage:
- background = (theEvent.message & resumeFlag);
- ActivateWindow(FrontWindow(), !background);
- break;
- }
- }
-
-
- void DispatchHighEvent(EventRecord theEvent)
- {
- // Send off those Apple Events!
-
- aeError = AEProcessAppleEvent(&theEvent);
- }
-